package com.sunlg.smarthome.ui.main;

import android.support.design.widget.TextInputEditText;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.presenter.RegisterPresenter;
import com.sunlg.smarthome.contract.RegisterContract;

import butterknife.BindView;

public class RegisterActivity extends BaseActivity
        implements RegisterContract.View, View.OnClickListener{

    @BindView(R.id.et_username)
    TextInputEditText etUserName;

    @BindView(R.id.et_password)
    TextInputEditText etPassword;

    @BindView(R.id.btn_register)
    Button btnRegister;

    private RegisterContract.Presenter registerPresenter;
    private String username;
    private String password;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_register;
    }

    @Override
    protected void init() {
        registerPresenter = new RegisterPresenter();
        registerPresenter.attachView(this);

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void registerEvents() {
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_register){
            username = etUserName.getText().toString();
            password = etPassword.getText().toString();
            if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
                etUserName.setError("用户名/密码不能为空");
            else
                registerPresenter.register(username, password);

        }
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("用户注册");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    public void showLoading() {
        showLoadBar("正在注册...");
    }

    @Override
    public void hideLoading() {
        hideLoadBar();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    public void forward() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        registerPresenter.dettachView();
    }
}
